package com.example.transactionhintassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.transactionhintassistant.mapper.TasksMapper;
import com.example.transactionhintassistant.pojo.entity.Task;
import com.example.transactionhintassistant.pojo.entity.WeeklyPlan;
import com.example.transactionhintassistant.pojo.vo.TaskVo;
import com.example.transactionhintassistant.pojo.weeklyplan.AddTaskByWeeklyPlanDto;
import com.example.transactionhintassistant.service.WeeklyPlansService;
import com.example.transactionhintassistant.mapper.WeeklyPlansMapper;
import com.example.transactionhintassistant.utils.JwtHelper;
import com.example.transactionhintassistant.utils.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 86147
* @description 针对表【weekly_plans(周计划表)】的数据库操作Service实现
* @createDate 2024-03-04 22:32:13
*/
@Service
public class WeeklyPlansServiceImpl extends ServiceImpl<WeeklyPlansMapper, WeeklyPlan>
    implements WeeklyPlansService{

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private WeeklyPlansMapper weeklyPlansMapper;
    @Autowired
    private TasksMapper tasksMapper;

    @Override
    public Result<Object> addWeeklyPlan(String planName) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        QueryWrapper<WeeklyPlan> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("plan_name",planName)
                .eq("user_id",userId);
        List<WeeklyPlan> weeklyPlanList = weeklyPlansMapper.selectList(queryWrapper);
        if (weeklyPlanList.size() == 0)
        {
            WeeklyPlan newWeeklyPlan = new WeeklyPlan();
            newWeeklyPlan.setPlanName(planName);
            newWeeklyPlan.setUserId(userId);
            this.save(newWeeklyPlan);
            return Result.ok(1,"成功添加周计划");
        }
        else {
            return Result.ok(2 ,"周计划名重复");
        }
    }

    @Override
    public Result<Object> changeWeeklyPlanName(int id,String planName) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        QueryWrapper<WeeklyPlan> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("plan_name",planName)
                .eq("user_id",userId);
        List<WeeklyPlan> weeklyPlanList = weeklyPlansMapper.selectList(queryWrapper);
        if (weeklyPlanList.size() == 0)
        {
            LambdaUpdateWrapper<WeeklyPlan> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(WeeklyPlan::getId,id)
                    .set(WeeklyPlan::getPlanName,planName);
            this.update(lambdaUpdateWrapper);
            return Result.ok(1,"修改周计划名成功");
        }
        else {
            return Result.ok(2 ,"周计划名重复");
        }



    }

    @Override
    public Result<Object> getWeeklyPlans() {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        LambdaQueryWrapper<WeeklyPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WeeklyPlan::getUserId,userId);
        List<WeeklyPlan> weeklyPlans = weeklyPlansMapper.selectList(lambdaQueryWrapper);
        return Result.ok(weeklyPlans,"返回个人周计划列表");
    }

    @Override
    public Result<Object> deleteWeeklyPlan(int id) {
        //找到事务表中的对应该周计划id的事务
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Task::getWeeklyPlanId,id);
        tasksMapper.delete(lambdaQueryWrapper);
        weeklyPlansMapper.deleteById(id);
        return Result.ok("删除周计划及事务成功");
    }

    @Override
    public Result<Object> addTaskByWeeklyPlan(AddTaskByWeeklyPlanDto addTaskByWeeklyPlanDto) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);

        Task task = new Task();
        BeanUtils.copyProperties(addTaskByWeeklyPlanDto,task);
        task.setUserId(userId);
        task.setCompletionStatus(0); //默认下状态为未完成
        task.setCreatedTime(new Date());
        tasksMapper.insert(task);
        return Result.ok(null,"周计划添加事务成功");
    }

    @Override
    public Result<Object> getWeeklyPlanIdByName(String planName) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);

        LambdaQueryWrapper<WeeklyPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WeeklyPlan::getPlanName,planName)
                .eq(WeeklyPlan::getUserId,userId);

        WeeklyPlan weeklyPlan = weeklyPlansMapper.selectOne(lambdaQueryWrapper);

        return Result.ok(weeklyPlan.getId(),"寻找周计划id");
    }

    @Override
    public Result<Object> getTaskFromWeeklyPlan(int id) {
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Task::getWeeklyPlanId,id);
        List<Task> taskList = tasksMapper.selectList(lambdaQueryWrapper);
        List<TaskVo> taskVoList = new ArrayList<>();
        for (Task task : taskList) {
            TaskVo taskVo = new TaskVo();
            BeanUtils.copyProperties(task, taskVo); // 将Task对象的属性复制到TaskVo对象中
            taskVoList.add(taskVo); // 添加到TaskVo列表中
        }
        return Result.ok(taskVoList,"返回周计划的事务成功~");
    }
}




